﻿#include "../../libcat/include/cat-module.hpp"
#include "../../libcat/include/util/json.h"
#include "../../liblive2d/include/catlive2d.hpp"

namespace {
	sf::CircleShape circle(150);
	int width=0, height = 0;
	int desktopWidth = 0, desktopHeight = 0;
	int x=0, y=0;
	sf::Text textForLock;
}

static float t = 0;

static void* my_tick(float time) {

	t += time;
	if (t > 1.0f) {
		circle.setFillColor(sf::Color(rand() % 255, rand() % 255, 0, 255));
		t = 0;
	}
	
	return NULL;
}

static void* my_mouse_move(const cat_mouse_move_event* m) {

	x = m->x;
	y = m->y;
	float cx = static_cast<float>(x) / static_cast<float>(desktopWidth);
	float cy = static_cast<float>(y) / static_cast<float>(desktopHeight);
	cat_live2d_on_mouse_move_callback(cx,cy);

	return NULL;
}

void* Init(cat_data_t* settings) {

	desktopWidth = GetSystemMetrics(SM_CXFULLSCREEN);
	desktopHeight = GetSystemMetrics(SM_CYFULLSCREEN);
	width = settings->json["decoration"]["window_size"][0].asInt();
	height = settings->json["decoration"]["window_size"][1].asInt();

	circle.setFillColor(sf::Color::Black);
	circle.setRadius(40);
	circle.setPosition(10, 20);

	cat_live2d_load_model("img/standard/cat_model/", "cat.model3.json");
	cat_live2d_resize(width, height);
	cat_live2d_scale(1.5,1.5);
	
	

	return settings;
}
void* Release() {
	return NULL;
};

void* Run(void* _window) {
	cat_window* cwindow = (cat_window*)_window;
	float cx = static_cast<float>(x) / static_cast<float>(desktopWidth)* (width-80);
	float cy = static_cast<float>(y) / static_cast<float>(desktopHeight) * (height-80);
	circle.setPosition(cx,cy);

	cat_live2d_run(cwindow);

	cwindow->window->draw(circle);

	textForLock.setFont(*cwindow->catfont);
	textForLock.setFillColor(sf::Color(255, 126, 126));
	textForLock.setCharacterSize(50);
	textForLock.setString(L"施工中");

	cwindow->window->draw(textForLock);

	

	return _window;
};

CAT_DECLARE_MODULE()
bool cat_module_load (){
	cat_source_info a {};
	a.id = 3;
	a.type = CAT_SOURCE_TYPE_MODE;
	a.Init = Init;
	a.Release = Release;
	a.Run = Run;
	a.Tick = my_tick;
	a.OnMouseMove = my_mouse_move;

	cat_register_source(&a);
	return true;
}
